NetLogo banner

Home
Download
Help
Forum
Resources
Extensions
FAQ
NetLogo Publications
Contact Us
Donate

Models:
Library
Community
Modeling Commons

Beginners Interactive NetLogo Dictionary (BIND)
NetLogo Dictionary

User Manuals:
Web
Printable
Chinese
Czech
Farsi / Persian
Japanese
Spanish

  Donate

NetLogo User Community Models

(back to the NetLogo User Community Models)

[screen shot]

Download
If clicking does not initiate a download, try right clicking or control clicking and choosing "Save" or "Download".

Try It in NetLogo Web

## WHAT IS IT?

This model demonstrates a simple 2D physics simulation in NetLogo. Each turtle has properties such as **mass**, **radius**, and **velocity**. The model applies:

- **Gravity**, pulling turtles downward,
- **Wall collisions**, which bounce the turtles off the edges of the world,
- **Turtle-turtle collisions**, modeling elastic collisions between circular objects.

It showcases how to update positions, velocities, and detect collisions in each time step to create a rudimentary “physics engine” in NetLogo.

## HOW IT WORKS

1. **Turtle Variables**
Each turtle has:
- `vx` / `vy`: Velocity in the x/y directions,
- `mass`: Used in collision calculations,
- `radius`: Used to check if two turtles overlap.

2. **Gravity**
A small negative constant is added to `vy` every tick, causing a downward acceleration.

3. **Movement**
Each turtle’s position (`xcor`, `ycor`) is updated by adding (`vx`, `vy`) once per tick.

4. **Boundary Collisions**
When a turtle crosses a boundary, it is pushed back into the valid area, and its velocity component (x or y) is reversed.

5. **Turtle-Turtle Collisions**
- The distance between two turtles is compared to the sum of their radii. If they overlap, they are first separated.
- Then, their velocities are updated using simplified **elastic collision** formulas based on their masses.

## HOW TO USE IT

1. **Buttons**
- **Setup**: Clears the world and creates a specified number of turtles with random positions, velocities, and masses.
- **Go**: Repeatedly applies gravity, moves turtles, and checks for collisions in each tick.
- **Up**: Add upward acceration to turtles
- **Mouse click**: Add Force to turtles which points at the mouse location

2. **Initial Conditions**
- By default, 20 turtles are created, each with random properties (mass, velocity, radius). You can modify these values in the code or by adding a slider in the Interface tab.

3. **Running the Model**
1. Click **Setup** to initialize.
2. Click **Go** to start the simulation.
3. Observe how turtles move, bounce off walls, and collide with each other.

## THINGS TO NOTICE

- **Mass Effect**: When a light turtle collides with a heavier one, the lighter turtle’s velocity changes more dramatically.
- **Energy Exchange**: In perfectly elastic collisions, kinetic energy is conserved. Turtles may exchange velocities but the total system energy (minus any gravity potential changes) remains roughly the same.
- **Gravity**: Notice how gravity accelerates turtles downward. They will bounce on the “floor” boundary repeatedly.

## THINGS TO TRY

1. **Vary Gravity**
Change the gravity constant in the code to see how stronger or weaker gravity affects the system.

2. **Inelastic Collisions**
Modify the collision formulas to remove some fraction of velocity (e.g., multiply velocities by 0.9) to simulate energy loss.

3. **Friction / Damping**
Multiply each velocity by a factor < 1 (e.g., 0.99) each tick to simulate drag or friction.

4. **Remove Gravity**
Comment out the `apply-gravity` procedure call so turtles only collide with each other and the walls.

5. **Increase Turtles**
Raise the number of turtles to see if performance changes or how collisions become more frequent.

## EXTENDING THE MODEL

- **Rotational Dynamics**: Give turtles angular velocity and calculate spin changes on collisions.
- **Gravitational Attraction**: Replace the constant downward gravity with pairwise Newtonian attraction so turtles orbit each other.
- **Spatial Partitioning**: For many turtles (hundreds or thousands), collision checks become expensive. Implement a grid or quadtree to reduce the computational load.

## NETLOGO FEATURES

- **Turtles**: Each turtle holds custom variables (`vx`, `vy`, `mass`, `radius`).
- **Patches**: Used here primarily for setting a background color.
- **Built-In Primitives**:
- `distance` for measuring the distance between turtles,
- `min-pxcor` / `max-pxcor` (and similarly `pycor`) for boundary detection.
- **Procedures**: The model splits actions into distinct procedures (`apply-gravity`, `move-turtles`, `handle-wall-collisions`, `handle-turtle-collisions`).

## RELATED MODELS

- **GasLab** models in the NetLogo Models Library, which simulate elastic collisions among particles in a 2D container.
- **Particle System** or other physical simulation models available in the library or on the NetLogo community website.

## CREDITS AND REFERENCES

- This code and explanation were adapted for demonstration purposes.
- NetLogo is created by Uri Wilensky and developed at the Center for Connected Learning and Computer-Based Modeling, Northwestern University.
- To learn more about NetLogo, visit the [NetLogo website](https://ccl.northwestern.edu/netlogo/).

(back to the NetLogo User Community Models)